package io.github.libkodi.vertxauth;

import java.lang.reflect.Method;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.github.libkodi.basic_gateway.router.AbstractRouteIntercepter;
import io.vertx.core.Future;
import io.vertx.ext.web.RoutingContext;

public class RequestAuthCenter extends AbstractRouteIntercepter {
	private static final Logger log = LoggerFactory.getLogger(RequestAuthCenter.class);
	
	private VertxWebAuth auth = null;

	@Override
	public void init() {
		ctx.components().get(VertxWebAuth.class).onSuccess(auth -> {
			this.auth = auth;
		}).onFailure(e -> {
			log.error("Cannot read component: io.github.libkodi.vertxauth.VertxWebAuth", e);
		});
	}
	
	@Override
	public Future<Void> request(RoutingContext context, Method method) {
		return Future.future(promise -> {
			if (auth != null) {
				auth.verify(context);
			}
			
			promise.complete();
		});
	}

}
